package com.android.basiclib.uitls;


import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 时间转换工具类
 */

public class DateAndTimeUtil {

    /**
     * 兼容php时间戳转换  dd MMMM, yyyy
     */
    public static String stampToDate1(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d MMM yyyy");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }


    //将时间戳转换成指定格式的时间字符串，调用此方法输入所要转换的时间戳输入例如（1402733340）输出（"MM/dd/yyyy HH:mm:ss"） 
    public static String formatLongToDateStr(String longStr, String formatType) {

        if (null == longStr || longStr.isEmpty()) {
            return null;
        }

//        Long dateInt =Long.valueOf(dateStr.trim());
//        int dateInt =Integer.valueOf(longStr.trim());
//        Date date= new Date(dateInt*1000L);

        long dateLong = Long.valueOf(longStr.trim());
        Date date = new Date(dateLong);

//        LogUtil.logE(tag,date.toString());

        SimpleDateFormat format = new SimpleDateFormat(formatType);
        String dateTemp = format.format(date);
//        LogUtil.logE(tag,"dateTemp="+dateTemp);
        return dateTemp;

    }


    //将时间字符串转换成时间戳
    public static String formatDateStrToLong(String dateStr, String formatType) {

        if (null == dateStr || null == formatType) {
            return "";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(formatType);
        Date date;
        String times = null;
        try {
            date = sdf.parse(dateStr);
            Long l = date.getTime()/1000;
//            String stf = String.valueOf(l);
//            times = stf.substring(0,10);
            times = String.valueOf(l);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return times;
    }


    /**
     * 兼容php时间戳转换  yyyy-MM-dd
     */
    public static String stampToDate2(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }


    /**
     * 兼容php时间戳转换 HH:mm a
     */
    public static String stampToDate3(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm a");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 兼容php时间戳转换 dd MMMM
     */
    public static String stampToDate4(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMMM");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 兼容php时间戳转换 dd/MM/yyyy HH:mm
     */
    public static String stampToDate5(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm a");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 兼容php时间戳转换 dd/MM/yyyy
     */
    public static String stampToDate6(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }


    /**
     * 兼容php时间戳转换 EE,dd/MM/yyyy
     */
    public static String stampToDate7(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EE,dd/MM/yyyy");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }


    /**
     * 兼容php时间戳转换 yyyyMMdd   只是自己数据库中查询保存的时候用
     */
    public static String stampToDate8(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 兼容php时间戳转换 EE,MMM d,yyyy
     */
    public static String stampToDate9(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EE, MMM d, yyyy");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }


    /**
     * 兼容php时间戳转换 HH:mm
     */
    public static String stampToDate10(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 兼容php时间戳转换 d MMM yyyy HH:mm
     */
    public static String stampToDate11(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 兼容php时间戳转换 HH:mm EE, MMM dd, yyyy
     */
    public static String stampToDate12(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm EE, MMM dd, yyyy");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 兼容php时间戳转换 MMM dd, yyyy HH:mm
     */
    public static String stampToDate13(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 兼容php时间戳转换 EE,MMM dd yyyy
     */
    public static String stampToDate14(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EE, MMM dd yyyy");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 兼容php时间戳转换 yyyy/MM/dd
     */
    public static String stampToDate15(String s) {
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 兼容php时间戳转换 HH
     */
    public static String stampToDate(String s, String formatType) {

        if (TextUtils.isEmpty(s) || TextUtils.isEmpty(formatType)) {

            return null;
        }
        //转换PHP时间戳
        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatType);
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 时间转换为时间戳
     *
     * @param timeStr 时间 例如: 2016-03-09
     * @param format  时间对应格式  例如: yyyy-MM-dd
     * @return
     */
    public static long getTimeStamp(String timeStr, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.UK);
        Date date = null;
        try {
            date = simpleDateFormat.parse(timeStr);
            long timeStamp = date.getTime();
            return timeStamp;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


    //日期转换成多少天前
    private static long minute = 1000 * 60;
    private static long hour = minute * 60;
    private static long day = hour * 24;
    private static long halfamonth = day * 15;
    private static long month = day * 30;

    public static String getDateDiff(String s) {

        if (s.length() < 13) {
            StringBuilder builder = new StringBuilder(s);
            builder.append("000");
            s = builder.toString();
        }

        Long dateTimeStamp = Long.parseLong(s);
        String result;
        long now = new Date().getTime();
        long diffValue = now - dateTimeStamp;
        if (diffValue < 0) {
            //toast("结束日期不能小于开始日期！");
        }
        long monthC = diffValue / month;
        long weekC = diffValue / (7 * day);
        long dayC = diffValue / day;
        long hourC = diffValue / hour;
        long minC = diffValue / minute;
        if (monthC >= 1) {
            result = Integer.parseInt(monthC + "") + " months ago";
            return result;
        } else if (weekC >= 1) {
            result = Integer.parseInt(weekC + "") + " weeks ago";
            return result;
        } else if (dayC >= 1) {
            result = Integer.parseInt(dayC + "") + " days ago";
            return result;
        } else if (hourC >= 1) {
            result = Integer.parseInt(hourC + "") + " hours ago";
            return result;
        } else if (minC >= 1) {
            result = Integer.parseInt(minC + "") + " minutes ago";
            return result;
        } else {
            result = "a moment ago";
            return result;
        }
    }
}
